import adminConfig from './admin'
import bannerConfig from './banner'
import authConfig from './auth'
import Utils from '@/lin/utils/util'

// eslint-disable-next-line import/no-mutable-exports
let homeRouter = [
  {
    title: '登录日志管理',
    type: 'view',
    name: Symbol('log'),
    route: '/log',
    filePath: 'views/log/Log.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
    right: ['登录日志'],
  },
  bannerConfig,
  {
    title: '官方活动管理',
    type: 'view',
    name: Symbol('gf-activity'),
    route: '/gf-activity',
    filePath: 'views/activity/GfActivity.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
    right: ['官方活动'],
  },
  {
    title: '查看活动商家',
    type: 'view',
    name: Symbol('sj-msg'),
    route: '/sj-msg',
    filePath: 'views/msg/SjMsg.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
    right: ['查看活动商家'],
  },
  {
    title: '商家活动管理',
    type: 'view',
    name: Symbol('sj-activity'),
    route: '/sj-activity',
    filePath: 'views/activity/SjActivity.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
    right: ['商家活动'],
  },
  {
    title: '个人活动管理',
    type: 'view',
    name: Symbol('gr-activity'),
    route: '/gr-activity',
    filePath: 'views/activity/GrActivity.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
    right: ['个人活动'],
  },
  {
    title: '信息设置',
    type: 'view',
    name: Symbol('system'),
    route: '/system',
    filePath: 'views/system/System.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
    right: ['信息设置'],
  },
  {
    title: '会员卡设置',
    type: 'view',
    name: Symbol('member-type'),
    route: '/member-type',
    filePath: 'views/member-type/MemberType.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
    right: ['会员卡设置'],
  },
  authConfig,
  {
    title: '用户上传管理',
    type: 'view',
    name: Symbol('user-auth'),
    route: '/user-auth',
    filePath: 'views/user/UserAuth.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
    right: ['上墙设置'],
  },
  {
    title: '用户商家管理',
    type: 'view',
    name: Symbol('user-auth-excel'),
    route: '/user-auth-excel',
    filePath: 'views/user/UserAuthExcel.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
    right: ['用户商家'],
  },
  // {
  //   title: '用户管理',
  //   type: 'view',
  //   name: Symbol('user'),
  //   route: '/user',
  //   filePath: 'views/user/User.vue',
  //   inNav: true,
  //   icon: 'iconfont icon-rizhiguanli',
  //   order: null,
  //   right: ['隐私设置'],
  // },
  // {
  //   title: '微信菜单管理',
  //   type: 'view',
  //   name: Symbol('menu'),
  //   route: '/menu',
  //   filePath: 'views/menu/Menu.vue',
  //   inNav: true,
  //   icon: 'iconfont icon-kecheng',
  //   order: null,
  //   right: ['微信菜单管理'],
  // },
  // {
  //   title: '微信自动回复',
  //   type: 'view',
  //   name: Symbol('reply'),
  //   route: '/reply',
  //   filePath: 'views/reply/Reply.vue',
  //   inNav: true,
  //   icon: 'iconfont icon-kecheng',
  //   order: null,
  //   right: ['微信自动回复'],
  // },
  adminConfig,
  {
    title: '404',
    type: 'view',
    name: Symbol('404'),
    route: '/404',
    filePath: 'views/error-page/404.vue',
    inNav: false,
    icon: 'iconfont icon-rizhiguanli',
  },
]

const plugins = []

// 筛除已经被添加的插件
function filterPlugin(data) {
  if (plugins.length === 0) {
    return
  }
  if (Array.isArray(data)) {
    data.forEach((item) => {
      filterPlugin(item)
    })
  } else {
    // eslint-disable-next-line arrow-parens
    const findResult = plugins.findIndex((item) => data === item)
    if (findResult >= 0) {
      plugins.splice(findResult, 1)
    }
    if (data.children) {
      filterPlugin(data.children)
    }
  }
}

filterPlugin(homeRouter)

homeRouter = homeRouter.concat(plugins)

// 处理顺序
homeRouter = Utils.sortByOrder(homeRouter)

// 使用 Symbol 处理 name 字段, 保证唯一性
const deepReduceName = (target) => {
  if (Array.isArray(target)) {
    target.forEach((item) => {
      if (typeof item !== 'object') {
        return
      }
      deepReduceName(item)
    })
    return
  }
  if (typeof target === 'object') {
    if (typeof target.name !== 'symbol') {
      // eslint-disable-next-line no-param-reassign
      target.name = target.name || Utils.getRandomStr()
      // eslint-disable-next-line no-param-reassign
      target.name = Symbol(target.name)
    }

    if (Array.isArray(target.children)) {
      target.children.forEach((item) => {
        if (typeof item !== 'object') {
          return
        }
        deepReduceName(item)
      })
    }
  }
}

deepReduceName(homeRouter)

export default homeRouter
